package com.darren.wordcount;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StatisticResultsCache {

	private static Map<String, Integer> statisticResultCache = new ConcurrentHashMap<String, Integer>();
	
	private StatisticResultsCache(){}
	
	
	public static void add(Map<String, Integer> result){
		Iterator<Map.Entry<String, Integer>> it = result.entrySet().iterator();
		
		while(it.hasNext()){
			Map.Entry<String, Integer> entry = it.next();
			String key =  entry.getKey();
			
			if(statisticResultCache.containsKey(entry.getKey()))
				statisticResultCache.put(key, statisticResultCache.get(key)+entry.getValue());
			else
				statisticResultCache.put(key, entry.getValue());
		}
	}
	
	
	public static void print(){
		Iterator<Map.Entry<String, Integer>> it = statisticResultCache.entrySet().iterator();
		
		while(it.hasNext()){
			Map.Entry<String, Integer> entry = it.next();
			String key =  entry.getKey();
			int value = entry.getValue();
			
			System.out.println(key+" "+value);
		}
		
		//System.out.println("print......"+statisticResultCache);
	}
}
